﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TradeMeSDK;

namespace TradeMo
{
    public class CriteriaJobsViewModel : BaseViewModel
    {
        private SearchJobsCriteria _criteria;
        public SearchJobsCriteria Criteria
        {
            get
            {
                if (_criteria == null)
                {
                    _criteria = new SearchJobsCriteria();
                    UpdateFields();
                }
                return _criteria;
            }
            set
            {
                _criteria = value;
                UpdateFields();
            }
        }

        ////////////////////////////////////////////////////////////////////////////

        private Locality _selectedRegion;
        public Locality SelectedRegion
        {
            get { return _selectedRegion; }
            set
            {
                _selectedRegion = value;
                NotifyPropertyChanged("RegionName");
                SelectedDistrict = null;
                Criteria.Region = _selectedRegion != null ? _selectedRegion.LocalityId : 0;
            }
        }

        public string RegionName
        {
            get
            {
                return SelectedRegion == null ? "New Zealand" : SelectedRegion.Name;
            }
        }

        public List<SelectorItem<Locality>> Regions
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                var list = new List<SelectorItem<Locality>>();
                list.Add(new SelectorItem<Locality> { Title = "New Zealand" });
                list.AddRange(
                    App.TradeMe.Catalog.Localities.Select(area => new SelectorItem<Locality> { Title = area.Name, Item = area }));
                return list.ToList();
            }
        }

        ///////////////////////////////////////////////////////////////////////////////

        private District _selectedDistrict;
        public District SelectedDistrict
        {
            get { return _selectedDistrict; }
            set
            {
                _selectedDistrict = value;
                NotifyPropertyChanged("DistrictName");
                NotifyPropertyChanged("DistrictVisible");
                Criteria.District = _selectedDistrict != null ? _selectedDistrict.DistrictId : 0;
            }
        }


        public bool DistrictVisible
        {
            get
            {
                if (SelectedRegion == null) return false;
                if (SelectedRegion.Districts == null) return false;
                return SelectedRegion.Districts.Length > 0;
            }
        }

        public string DistrictName
        {
            get
            {
                return SelectedDistrict == null ? "All districts" : SelectedDistrict.Name;
            }
        }

        public List<SelectorItem<District>> Districts
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                if (!DistrictVisible) return null;
                var list = new List<SelectorItem<District>>();
                list.Add(new SelectorItem<District> { Title = "All districts" });
                list.AddRange(
                    SelectedRegion.Districts.Select(
                        district => new SelectorItem<District> { Title = district.Name, Item = district }));
                return list.ToList();
            }
        }

        ////////////////////////////////////////////////////////////////////////////

        private JobCategory _selectedJobCategory;
        public JobCategory SelectedJobCategory
        {
            get { return _selectedJobCategory; }
            set
            {
                _selectedJobCategory = value;
                NotifyPropertyChanged("JobCategoryName");
                SelectedJobSubCategory = null;
                Criteria.Category = _selectedJobCategory != null ? Convert.ToInt32(_selectedJobCategory.Code) : 0;
            }
        }

        public string JobCategoryName
        {
            get
            {
                return SelectedJobCategory == null ? "All Categories" : SelectedJobCategory.Name;
            }
        }

        public List<SelectorItem<JobCategory>> JobCategories
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                var list = new List<SelectorItem<JobCategory>>();
                list.Add(new SelectorItem<JobCategory> { Title = "All Categories" });
                list.AddRange(
                    App.TradeMe.Catalog.JobCategories.Select(
                        category => new SelectorItem<JobCategory> { Title = category.Name, Item = category }));
                return list.ToList();
            }
        }

        ///////////////////////////////////////////////////////////////////////////////

        private JobCategory _selectedJobSubCategory;
        public JobCategory SelectedJobSubCategory
        {
            get { return _selectedJobSubCategory; }
            set
            {
                _selectedJobSubCategory = value;
                NotifyPropertyChanged("JobSubCategoryName");
                NotifyPropertyChanged("JobSubCategoryVisible");
                Criteria.Category = _selectedJobSubCategory != null ? Convert.ToInt32(_selectedJobSubCategory.Code) : 0;
            }
        }


        public bool JobSubCategoryVisible
        {
            get
            {
                if (SelectedJobCategory == null) return false;
                if (SelectedJobCategory.SubCategories == null) return false;
                return SelectedJobCategory.SubCategories.Count > 0;
            }
        }

        public string JobSubCategoryName
        {
            get
            {
                if (SelectedJobSubCategory == null) return "All";
                return SelectedJobSubCategory.Name;
            }
        }

        public List<SelectorItem<JobCategory>> JobSubCategories
        {
            get
            {
                if (!App.TradeMe.Catalog.IsDownloaded) return null;

                if (!JobSubCategoryVisible) return null;
                var list = new List<SelectorItem<JobCategory>>();
                list.Add(new SelectorItem<JobCategory> { Title = "All" });
                list.AddRange(
                    SelectedJobCategory.SubCategories.Select(
                        category => new SelectorItem<JobCategory> { Title = category.Name, Item = category }));
                return list.ToList();
            }
        }

        /////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedPayFrom;
        public SelectorItem<string> SelectedPayFrom
        {
            get
            {
                return _selectedPayFrom ?? (_selectedPayFrom = new SelectorItem<string> { Title = "Any" });
            }
            set
            {
                _selectedPayFrom = value;
                NotifyPropertyChanged("PayFromName");
                Criteria.MinSalary = _selectedPayFrom.Item != null ? Convert.ToDecimal(_selectedPayFrom.Item) : 0;
            }
        }
        public string PayFromName
        {
            get { return SelectedPayFrom.Title; }
            set { SelectedPayFrom.Title = value; }
        }

        private SelectorItem<string> _selectedPayTo;
        public SelectorItem<string> SelectedPayTo
        {
            get
            {
                return _selectedPayTo ?? (_selectedPayTo = new SelectorItem<string> { Title = "Any" });
            }
            set
            {
                _selectedPayTo = value;
                NotifyPropertyChanged("PayToName");
                Criteria.MaxSalary = _selectedPayTo != null ? Convert.ToDecimal(_selectedPayTo.Item) : 0;
            }
        }
        public string PayToName
        {
            get { return SelectedPayTo.Title; }
            set { SelectedPayTo.Title = value; }
        }

        public List<SelectorItem<string>> PayItems
        {
            get
            {
                var list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Item="0", Title = "Any"},
                        new SelectorItem<string> {Item="0", Title = "$0"},
                        new SelectorItem<string> {Item="20000", Title = "$20k"},
                        new SelectorItem<string> {Item="25000", Title = "$25k"},
                        new SelectorItem<string> {Item="30000", Title = "$30k"},
                        new SelectorItem<string> {Item="35000", Title = "$35k"},
                        new SelectorItem<string> {Item="40000", Title = "$40k"},
                        new SelectorItem<string> {Item="50000", Title = "$50k"},
                        new SelectorItem<string> {Item="60000", Title = "$60k"},
                        new SelectorItem<string> {Item="70000", Title = "$70k"},
                        new SelectorItem<string> {Item="80000", Title = "$80k"},
                        new SelectorItem<string> {Item="90000", Title = "$90k"},
                        new SelectorItem<string> {Item="100000", Title = "$100k+"},
                    };

                return list;
            }
        }

        /////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedJobType;
        public SelectorItem<string> SelectedJobType
        {
            get
            {
                return _selectedJobType ?? (_selectedJobType = new SelectorItem<string> { Title = "Any job type" });
            }
            set
            {
                _selectedJobType = value;
                NotifyPropertyChanged("JobTypeName");
                Criteria.PositionType = _selectedJobType != null ? _selectedJobType.Item : null;
            }
        }
        public string JobTypeName
        {
            get { return SelectedJobType.Title; }
            set { SelectedJobType.Title = value; }
        }
        public string JobTypeItem
        {
            get { return Criteria.PositionType; }
            set { Criteria.PositionType = value; }
        }

        public List<SelectorItem<string>> JobTypeItems
        {
            get
            {
                List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Title = "Any job type", Item = ""},
                        new SelectorItem<string> {Title = "Contract/temp", Item = "CT"},
                        new SelectorItem<string> {Title = "Part time", Item = "PT"},
                        new SelectorItem<string> {Title = "Full time", Item = "FT"},
                    };

                return list;
            }
        }

        ////////////////////////////////////////////////////////////////////////////

        private SelectorItem<string> _selectedSortBy;
        public SelectorItem<string> SelectedSortBy
        {
            get
            {
                return _selectedSortBy ?? (_selectedSortBy = new SelectorItem<string> { Title = "Default" });
            }
            set
            {
                _selectedSortBy = value;
                NotifyPropertyChanged("SortByName");
                Criteria.SortOrder = _selectedSortBy != null ? _selectedSortBy.Item : null;
            }
        }
        public string SortByName
        {
            get { return SelectedSortBy.Title; }
            set { SelectedSortBy.Title = value; }
        }

        public string SortByItem
        {
            get { return Criteria.SortOrder; }
            set { Criteria.SortOrder = value; }
        }

        public List<SelectorItem<string>> SortByItems
        {
            get
            {
                List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Title = "Default", Item = ""},
                        new SelectorItem<string> {Title = "Title", Item = "TitleAsc"},
                        new SelectorItem<string> {Title = "Latest Listings", Item = "ExpiryDesc"},
                    };

                return list;
            }
        }

        /////////////////////////////////////////////////////////////////////

        public void UpdateFields()
        {
            int district = Criteria.District;
            if (Criteria.Region != 0)
            {
                SelectedRegion = App.TradeMe.Catalog.Localities.FirstOrDefault(l => l.LocalityId == Criteria.Region);
                if (district != 0)
                    SelectedDistrict = SelectedRegion.Districts.FirstOrDefault(d => d.DistrictId == district);
            }
            if (Criteria.Category != 0)
            {
                int subCategory = Criteria.SubCategory;
                SelectedJobCategory = App.TradeMe.Catalog.JobCategories
                                         .FirstOrDefault(j => Convert.ToInt32(j.Code) == Criteria.Category);
                if (subCategory != 0)
                {
                    SelectedJobSubCategory = SelectedJobCategory.SubCategories
                                        .FirstOrDefault(j => Convert.ToInt32(j.Code) == subCategory);
                }
            }

            if (Criteria.MinSalary > 0)
                SelectedPayFrom = PayItems.FirstOrDefault(p => p.Item == Criteria.MinSalary.ToString());
            if (Criteria.MaxSalary > 0)
                SelectedPayTo = PayItems.FirstOrDefault(p => p.Item == Criteria.MaxSalary.ToString());

            if (Criteria.PositionType != null)
                SelectedJobType = JobTypeItems.FirstOrDefault(t => t.Item == Criteria.PositionType);

            if (!string.IsNullOrEmpty(Criteria.SortOrder))
                SelectedSortBy = SortByItems.FirstOrDefault(s => s.Item == Criteria.SortOrder);
        }

        public void Reset()
        {
            SelectedRegion = null;
            SelectedJobType = null;
            SelectedSortBy = null;
            SelectedJobCategory = null;
            SelectedPayFrom = null;
            SelectedPayTo = null;
            UpdateFields();
        }

    }
}
